package com.feeyin.radar.listener;

import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.sys.dao.OnlineUserDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.OnlineUser;
import com.feeyin.radar.sys.model.Users;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class OnlineUserListener
  implements HttpSessionBindingListener
{
  private static Map sessions = new HashMap();
  private OnlineUserDAO onlineUserDAO;
  private UserDAO userDAO;

  public UserDAO getUserDAO()
  {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public OnlineUserDAO getOnlineUserDAO() {
    return this.onlineUserDAO;
  }

  public void setOnlineUserDAO(OnlineUserDAO onlineUserDAO) {
    this.onlineUserDAO = onlineUserDAO;
  }

  public int getCount()
  {
    return this.onlineUserDAO.getPageOnlineUsersCount().intValue();
  }

  public boolean existUser(Integer userId, HttpSession session)
  {
    if (userId == null) return false;

    OnlineUser onlineUser = new OnlineUser();
    onlineUser = this.onlineUserDAO.getOnlineUserByUserId(userId);

    if (onlineUser == null) return false;
    ServletContext application = session.getServletContext();

    if (application.getAttribute("userList") != null) {
      Map userList = (HashMap)application.getAttribute("userList");

      if (userList.get(userId.toString()) != null) {
        HttpSession oldSession = (HttpSession)userList.get(userId.toString());

        if (!oldSession.getId().equals(session.getId())) {
          boolean result = deleteUser(userId, session);
          if (result) return false;
        }
      }
    }
    return true;
  }

  public boolean deleteUser(Integer userId, HttpSession session)
  {
    if (userId == null) return false;
    if (session == null) return false;
    boolean result = false;
    result = this.onlineUserDAO.deleteOnlineUser(userId);

    ServletContext application = session.getServletContext();
    if ((result) && (application.getAttribute("userList") != null)) {
      Map userList = (HashMap)application.getAttribute("userList");
      if (userList.get(userId.toString()) != null) {
        HttpSession oldSession = (HttpSession)userList.get(userId.toString());
        oldSession.invalidate();
        userList.remove(userId);
        this.userDAO.updateUserLastLoginDate(userId);
      }
    }

    return result;
  }

  public void valueBound(HttpSessionBindingEvent arg0) {
    HttpSession session = arg0.getSession();
    ServletContext application = session.getServletContext();
    if (sessions.size() == 0) {
      application.setAttribute("userList", sessions);
    }

    String userId = arg0.getName();

    if ((!StringUtils.isEmpty(userId)) && (!existUser(Integer.valueOf(Integer.parseInt(userId)), session))) {
      Users oldUser = new Users();
      oldUser = this.userDAO.getUser(Integer.valueOf(Integer.parseInt(userId)));
      if (oldUser != null) {
        OnlineUser onlineUser = new OnlineUser();
        onlineUser.setUserId(oldUser.getId());
        onlineUser.setUserCode(oldUser.getUserCode());
        onlineUser.setUserName(oldUser.getUserName());
        onlineUser.setLoginDate(oldUser.getLoginDate());
        onlineUser.setSessionId(session.getId());
        this.onlineUserDAO.insertOnlineUser(onlineUser);
        sessions.put(userId.toString(), session);
      }
    }
  }

  public void valueUnbound(HttpSessionBindingEvent arg0)
  {
    HttpSession session = arg0.getSession();
    String userId = arg0.getName();
    if (!StringUtils.isEmpty(userId)) {
      this.onlineUserDAO.deleteOnlineUser(Integer.valueOf(Integer.parseInt(userId)));
      this.userDAO.updateUserLastLoginDate(Integer.valueOf(Integer.parseInt(userId)));
    }
  }
}